using CCWin;
using ERP_SaaS_WinForm.物料管理.Material;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace ERP_SaaS_WinForm.物料管理
{
    public partial class SelectTaskForm : CCSkinMain
    {
        private List<RequiredMaterial> taskList;
        private RequiredMaterial selectedTask;
        
        /// <summary>
        /// 获取选中的任务
        /// </summary>
        public RequiredMaterial SelectedTask => selectedTask;
        
        public SelectTaskForm(List<RequiredMaterial> tasks)
        {
            InitializeComponent();
            this.taskList = tasks ?? new List<RequiredMaterial>();
            InitializeDataGridView();
            LoadTaskData();
        }
        
        /// <summary>
        /// 初始化数据网格
        /// </summary>
        private void InitializeDataGridView()
        {
            if (dgvTasks != null)
            {
                dgvTasks.AutoGenerateColumns = false;
                dgvTasks.AllowUserToAddRows = false;
                dgvTasks.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvTasks.MultiSelect = false;
                dgvTasks.ReadOnly = true;
                
                // 清空现有列
                dgvTasks.Columns.Clear();
                
                // 添加新的列，按照您要求的格式
                dgvTasks.Columns.Add(new DataGridViewTextBoxColumn
                {
                    Name = "taskCode",
                    HeaderText = "任务编号",
                    DataPropertyName = "taskCode",
                    Width = 100
                });
                
                dgvTasks.Columns.Add(new DataGridViewTextBoxColumn
                {
                    Name = "taskName",
                    HeaderText = "任务名称",
                    DataPropertyName = "taskName",
                    Width = 150
                });
                
                dgvTasks.Columns.Add(new DataGridViewTextBoxColumn
                {
                    Name = "productName",
                    HeaderText = "产品名称",
                    DataPropertyName = "productName",
                    Width = 120
                });
                
                dgvTasks.Columns.Add(new DataGridViewTextBoxColumn
                {
                    Name = "productId",
                    HeaderText = "产品编号",
                    DataPropertyName = "productId",
                    Width = 80
                });
                
                dgvTasks.Columns.Add(new DataGridViewTextBoxColumn
                {
                    Name = "specification",
                    HeaderText = "规格型号",
                    DataPropertyName = "specification",
                    Width = 100
                });
                
                dgvTasks.Columns.Add(new DataGridViewTextBoxColumn
                {
                    Name = "unitName",
                    HeaderText = "单位",
                    DataPropertyName = "unitName",
                    Width = 60
                });
                
                dgvTasks.Columns.Add(new DataGridViewTextBoxColumn
                {
                    Name = "taskState",
                    HeaderText = "状态",
                    DataPropertyName = "taskState",
                    Width = 80
                });
                
                // 为双击事件添加处理
                dgvTasks.CellDoubleClick += DgvTasks_CellDoubleClick;
            }
        }
        
        /// <summary>
        /// 加载任务数据
        /// </summary>
        private void LoadTaskData()
        {
            if (dgvTasks != null)
            {
                dgvTasks.DataSource = null;
                
                // 创建显示用的数据列表，包含状态文本
                var displayList = taskList.Select(task => new
                {
                    taskCode = task.taskCode,
                    taskName = task.taskName,
                    productName = task.productName,
                    productId = task.productId,
                    specification = task.specification,
                    unitName = task.unitName,
                    taskState = GetTaskStateText(task.taskState),
                    // 保存原始数据用于选择
                    OriginalTask = task
                }).ToList();
                
                dgvTasks.DataSource = displayList;
            }
        }
        
        /// <summary>
        /// 获取任务状态文本
        /// </summary>
        private string GetTaskStateText(int taskState)
        {
            switch (taskState)
            {
                case 1: return "待排产";
                case 2: return "已排产";
                case 3: return "进行中";
                case 4: return "已完成";
                case 5: return "已暂停";
                default: return "未知状态";
            }
        }
        
        /// <summary>
        /// 数据网格双击事件
        /// </summary>
        private void DgvTasks_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                SelectCurrentTask();
            }
        }
        
        /// <summary>
        /// 选择当前任务
        /// </summary>
        private void SelectCurrentTask()
        {
            if (dgvTasks.CurrentRow != null)
            {
                // 从显示数据中获取原始任务对象
                var displayItem = dgvTasks.CurrentRow.DataBoundItem;
                if (displayItem != null)
                {
                    // 使用反射获取OriginalTask属性
                    var originalTaskProperty = displayItem.GetType().GetProperty("OriginalTask");
                    if (originalTaskProperty != null)
                    {
                        selectedTask = originalTaskProperty.GetValue(displayItem) as RequiredMaterial;
                        if (selectedTask != null)
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                    }
                }
            }
        }
        
        /// <summary>
        /// 确认按钮点击事件
        /// </summary>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            SelectCurrentTask();
        }
        
        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        
        
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        private void SelectTaskForm_Load(object sender, EventArgs e)
        {
            // 窗体加载时的初始化
        }
    }
} 